# @Author: Eric Ito
# @Date: 1/29/2009
# @Name: Project Euler Problem 33


"""
Find all fractions such as 49/98 in which cancelling out one number
from numerator and denominator result in a simplified fractions

30/50 = 3/5 is trivial, so exclude it

less than 1 and 2 numbers in each numerator and denomincatior
numerator -> 10-98
denominator -> 11-99
"""

from util import isPrime

def equiv(n1,d1,n2,d2):
    if d1 == "0" or d2 == "0":
        return False
    return float(n1)/float(d1) == float(n2)/float(d2)

def gcd(a,b):
    if b % a == 0:
       return a
    multB = b
    multA = a
    while 1:
        if multA > multB:
            multB += b
        elif multA < multB:
            multA += a
        else:
            return (a*b)/multA


def main():
    NUM = 1
    DEN = 1
    for a in range(10,99):
        for b in range(11,100):
            origA = str(a)
            origB = str(b)
            if (a%10 + b%10) == 0:
                continue
            elif a/b >= 1:
                continue
            elif isPrime(a) and isPrime(b):
                continue
            else:
                bl = False
                if origA[0] == origB[0]:
                   bl = equiv(a,b,origA[1],origB[1])
                elif origA[1] == origB[0]:
                   bl = equiv(a,b,origA[0],origB[1])
                elif origA[0] == origB[1]:
                   bl = equiv(a,b,origA[1],origB[0])
                elif origA[1] == origB[1]:
                   bl = equiv(a,b,origA[1],origB[1])
                if bl:
                    NUM *= a
                    DEN *= b
    gcDiv = gcd(NUM,DEN)
    print DEN/gcDiv



if __name__ == "__main__":
    main()